#include "assessmentdetailview.h"
#include "employeemanager.h"
#include <QHeaderView>
#include <QMessageBox>
#include <QDebug>

AssessmentDetailView::AssessmentDetailView(EmployeeManager *manager, const QString &empId, QWidget *parent) : QWidget(parent),
    m_manager(manager),
    m_empId(empId),
    m_assessmentTable(nullptr),
    m_statusLabel(nullptr),
    m_startDateEdit(nullptr),
    m_endDateEdit(nullptr),
    m_searchButton(nullptr),
    m_periodComboBox(nullptr)
{
    initUI();
    if (!m_empId.isEmpty()) {
        loadAssessmentDetails();
    }
}

AssessmentDetailView::~AssessmentDetailView()
{
    // 析构函数，释放动态分配的内存
    delete m_assessmentTable;
    delete m_statusLabel;
    delete m_startDateEdit;
    delete m_endDateEdit;
    delete m_searchButton;
    delete m_periodComboBox;
}

void AssessmentDetailView::setEmployeeId(const QString &empId)
{
    m_empId = empId;
    loadAssessmentDetails();
}

void AssessmentDetailView::initUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建搜索区域
    QWidget *searchWidget = new QWidget(this);
    QHBoxLayout *searchLayout = new QHBoxLayout(searchWidget);

    // 期间选择下拉框
    m_periodComboBox = new QComboBox(this);
    m_periodComboBox->addItem("全部");
    m_periodComboBox->addItem("近一个月");
    m_periodComboBox->addItem("近三个月");
    m_periodComboBox->addItem("近半年");
    m_periodComboBox->addItem("近一年");
    m_periodComboBox->addItem("自定义");

    // 日期选择控件
    m_startDateEdit = new QDateEdit(this);
    m_startDateEdit->setCalendarPopup(true);
    m_startDateEdit->setDate(QDate::currentDate().addMonths(-1));
    m_endDateEdit = new QDateEdit(this);
    m_endDateEdit->setCalendarPopup(true);
    m_endDateEdit->setDate(QDate::currentDate());

    // 搜索按钮
    m_searchButton = new QPushButton("搜索", this);
    connect(m_searchButton, &QPushButton::clicked, this, &AssessmentDetailView::onSearchAssessment);
    connect(m_periodComboBox, &QComboBox::currentTextChanged, this, &AssessmentDetailView::onPeriodChanged);

    // 添加到搜索布局
    searchLayout->addWidget(new QLabel("期间: ", this));
    searchLayout->addWidget(m_periodComboBox);
    searchLayout->addWidget(m_searchButton);
    searchLayout->addStretch();

    // 创建考核表格
    m_assessmentTable = new QTableWidget(this);
    m_assessmentTable->setColumnCount(9);
    m_assessmentTable->setHorizontalHeaderLabels({"考核ID", "员工ID", "迟到次数", "请假次数", "早退次数", "迟到且早退次数", "缺勤次数", "考核分数", "考核周期"});
    m_assessmentTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_assessmentTable->setEditTriggers(QTableWidget::NoEditTriggers);

    // 状态标签
    m_statusLabel = new QLabel("请选择查询条件", this);

    // 添加到主布局
    mainLayout->addWidget(searchWidget);
    mainLayout->addWidget(m_assessmentTable);
    mainLayout->addWidget(m_statusLabel);

    setLayout(mainLayout);
    setWindowTitle("考核详情");
    resize(800, 600);
}

void AssessmentDetailView::loadAssessmentDetails()
{
    if (m_manager == nullptr || m_empId.isEmpty()) {
        m_statusLabel->setText("未设置员工信息");
        return;
    }

    // 默认选择全部
    m_periodComboBox->setCurrentIndex(0);
    // 调用搜索方法
    onSearchAssessment();
}

void AssessmentDetailView::onPeriodChanged(const QString &text)
{
    bool isCustom = (text == "自定义");
    m_startDateEdit->setEnabled(isCustom);
    m_endDateEdit->setEnabled(isCustom);
}

void AssessmentDetailView::onSearchAssessment()
{
    if (m_manager == nullptr || m_empId.isEmpty()) {
        m_statusLabel->setText("未设置员工信息");
        return;
    }

    int periodIndex = m_periodComboBox->currentIndex();
    QDate startDate, endDate;
    QDate currentDate = QDate::currentDate();
    QVariantList assessmentDetails;

    switch (periodIndex) {
    case 0: // 全部
        assessmentDetails = m_manager->getAssessmentDetails(m_empId);
        break;
    case 1: // 近一个月
        startDate = currentDate.addMonths(-1);
        assessmentDetails = m_manager->getAssessmentDetailsByDateRange(m_empId, startDate.toString("yyyy-MM-dd"), currentDate.toString("yyyy-MM-dd"));
        break;
    case 2: // 近三个月
        startDate = currentDate.addMonths(-3);
        assessmentDetails = m_manager->getAssessmentDetailsByDateRange(m_empId, startDate.toString("yyyy-MM-dd"), currentDate.toString("yyyy-MM-dd"));
        break;
    case 3: // 近半年
        startDate = currentDate.addMonths(-6);
        assessmentDetails = m_manager->getAssessmentDetailsByDateRange(m_empId, startDate.toString("yyyy-MM-dd"), currentDate.toString("yyyy-MM-dd"));
        break;
    case 4: // 近一年
        startDate = currentDate.addYears(-1);
        assessmentDetails = m_manager->getAssessmentDetailsByDateRange(m_empId, startDate.toString("yyyy-MM-dd"), currentDate.toString("yyyy-MM-dd"));
        break;
    case 5: // 自定义
        startDate = m_startDateEdit->date();
        endDate = m_endDateEdit->date();
        assessmentDetails = m_manager->getAssessmentDetailsByDateRange(m_empId, startDate.toString("yyyy-MM-dd"), endDate.toString("yyyy-MM-dd"));
        break;
    default:
        assessmentDetails = m_manager->getAssessmentDetails(m_empId);
        break;
    }

    // 清空表格
    m_assessmentTable->clearContents();
    m_assessmentTable->setRowCount(0);

    // 填充表格
    if (assessmentDetails.isEmpty()) {
        m_statusLabel->setText("没有找到考核记录");
        return;
    }

    m_assessmentTable->setRowCount(assessmentDetails.size());

    for (int i = 0; i < assessmentDetails.size(); ++i) {
        QVariantHash assessmentInfo = assessmentDetails[i].toHash();

        // 填充表格行
        m_assessmentTable->setItem(i, 0, new QTableWidgetItem(assessmentInfo["eval_id"].toString()));
        m_assessmentTable->setItem(i, 1, new QTableWidgetItem(assessmentInfo["emp_id"].toString()));
        m_assessmentTable->setItem(i, 2, new QTableWidgetItem(assessmentInfo["late_count"].toString()));
        m_assessmentTable->setItem(i, 3, new QTableWidgetItem(assessmentInfo["leave_count"].toString()));
        m_assessmentTable->setItem(i, 4, new QTableWidgetItem(assessmentInfo["early_leave_count"].toString()));
        m_assessmentTable->setItem(i, 5, new QTableWidgetItem(assessmentInfo["late_and_leave_count"].toString()));
        m_assessmentTable->setItem(i, 6, new QTableWidgetItem(assessmentInfo["absent_count"].toString()));
        m_assessmentTable->setItem(i, 7, new QTableWidgetItem(assessmentInfo["score"].toString()));
        m_assessmentTable->setItem(i, 8, new QTableWidgetItem(assessmentInfo["eval_period"].toString()));
    }

    m_statusLabel->setText(QString("共找到 %1 条考核记录").arg(assessmentDetails.size()));
}
